package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.LoginThreadLocal;
import com.project.dto.SupplierVerifyResultDto;
import com.project.entity.SupplierEntity;
import com.project.entity.SupplierRecordEntity;
import com.project.entity.UserRecordEntity;
import com.project.mapper.ISupplierMapper;
import com.project.mapper.ISupplierRecordMapper;
import com.project.service.ISupplierService;
import com.project.util.ConvertBeanUtil;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    private ISupplierMapper mapper;

    @Autowired
    private ISupplierRecordMapper recordMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

       @Override
    public IPage<SupplierEntity> findByItem(int pageNO, int status,int pageSize) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.eq("s_status", status);
        return mapper.selectPage(new Page<>(pageNO,pageSize),qw);
    }

    @Override
    public SupplierRecordEntity findByRef(Integer ref) {
        QueryWrapper<SupplierRecordEntity> qw = new QueryWrapper<>();
        qw.eq("r_supplierRef", ref);
        return recordMapper.selectOne(qw);
    }

    @Override
    public List<SupplierRecordEntity> findByHistory(long supplierRef) {
        QueryWrapper<SupplierRecordEntity> qw = new QueryWrapper<>();
        qw.eq("r_supplierRef", supplierRef);
        qw.ne("r_status", 0);
        return recordMapper.selectList(qw);
    }

    @Override
    public void updateStatus(Integer supplierRef, int status, String cause) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.eq("s_supplierRef", supplierRef);
        SupplierEntity supplier = mapper.selectOne(qw);

        QueryWrapper<SupplierRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("r_supplierRef", supplierRef);
        SupplierRecordEntity recordEntity = recordMapper.selectOne(queryWrapper);

        supplier.setStatus(status);
        mapper.updateById(supplier);
        recordEntity.setStatus(status);
        recordMapper.updateById(recordEntity);
        if (status != 1) {
            recordEntity.setCause(cause);
        }
        recordEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        recordMapper.updateById(recordEntity);


        SupplierVerifyResultDto dto = ConvertBeanUtil.copy(recordEntity, SupplierVerifyResultDto.class);
        //Java对象转化为JSON对象
        String jsonObject =  JSONObject.toJSONString(dto);
        rabbitTemplate.convertAndSend("topicExchangeTwo","rabbit.SupplierVerifyResult",jsonObject);
    }

    @Override
    public List<SupplierEntity> findAllSupplier() {
        return mapper.selectList(null);
    }
}
